<?php

namespace AuctionBack\Controller;

use AuctionCore\AntNest\Pay;
use AuctionCore\Common\AjaxPage;

class OilPayOrderController extends BaseController
{
    private $oilPayOrderLogic;

    private $empId;

    public function __construct()
    {
        parent::__construct();

        $this->oilPayOrderLogic = D('OilPayOrder', 'Logic');

        $this->empId = $this->user_info['emp_id'];
    }

    /**
     * 加油卡管理
     */
    public function index()
    {
        $this->display();
    }

    /**
     * 获取订单列表
     */
    public function getOrderList()
    {
        $p = I('request.p');
        $pageSize = 10;
        $status = I('request.status', 1);
        $condition = ['status' => $status];
        $condition['order_sn'] = I('request.order_sn');
        $condition['juhe_order_sn'] = I('request.juhe_order_sn');
        $condition['pay_code'] = I('request.pay_code');
        $condition['card_type'] = I('request.card_type');
        $condition['card_num'] = I('request.card_num');
        $condition['user_id'] = I('request.user_id');
        $condition['nick_name'] = I('request.user_name');
        $condition['start_time'] = I('request.start_time');
        $condition['end_time'] = I('request.end_time');

        $data = $this->oilPayOrderLogic->getOrderList($p, $pageSize, $condition);

        $Page = new AjaxPage($data['total'], $pageSize, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();// 分页显示输出

        $this->assign('status', $status);
        $this->assign('page', $show);
        $this->assign('list', $data['list']);
        $this->display();
    }


    /**
     * 加油卡充值订单详情
     */
    public function orderDetail()
    {

        $id = I('get.id');

        $order = $this->oilPayOrderLogic->getOrderById($id);

        $user = D('ClcwUser', 'Logic')->getUserInfoByUid($order['user_id']);

        $employee = M('employee')->where(['emp_id' => $order['emp_id']])->find();

        $this->assign('user', $user);
        $this->assign('employee', $employee);
        $this->assign('order', $order);
        $this->display();

    }


    public function getOrderInfoJson()
    {
        $id = I('post.id');

        $data = $this->oilPayOrderLogic->getOrderById($id, 'id,order_sn,pay_sn,pay_code,money');

        switch ($data['pay_code']) {
            case 'ALIPAY':
                $data['pay_type'] = '支付宝';
                break;
            case 'WEIXIN':
                $data['pay_type'] = '微信';
                break;
            case 'LAKALA':
                $data['pay_type'] = '拉卡拉';
                break;
        }

        $data['order_amount'] = $data['money'];

        echo json_encode($data);

    }

    /**
     * 退款操作
     */
    public function oilPayOrderRefund()
    {
        $id = I('post.id/d', 0);
        if ($id < 1) {
            $this->helpJsonResult('400', '请求参数错误');
        }
        $detail = $this->oilPayOrderLogic->getOrderById($id);
        $this->oilPayOrderLogic->saveOrderById($id,
            [
                'emp_id'      => $this->empId,
                'update_time' => NOW_TIME
            ]
        );
        if ($detail['status'] != 4) {
            $this->helpJsonResult('400', '该订单没有激活退款');
        }
        $refund_notice_url = getCurHostUrl() . U('Notify/OilPayRefund');

        $antPay = new Pay();
        $result = $antPay->refund($detail['pay_no'], $detail['pay_sn'], $refund_notice_url,
            '加油卡充值订单' . $detail['pay_no'] . '退款');
        if (!$result) {
            $this->helpJsonResult('400', '退款请求失败');
        }
        $this->helpJsonResult('200', '请求成功', $result['refund_url']);
    }
}